import employeesobj from '@/api//constant/employees'
function fn(dom, obj) {
  let resultItme = ''
  employeesobj.hireType.forEach((item) => {
    if (item.id === obj.value) {
      resultItme = item
    }
  })
  const resultStr = resultItme === '' ? '-' : resultItme.value
  dom.innerText = resultStr
}
let clickFn = ''
const directiveObj = {
  imagerror: {
    inserted(dom, obj, vnode) {
      // 只执行一次
      obj.value = obj.value || require('@/assets/common/bigUserHeader.png')
      dom.src = dom.src || obj.value
      dom.onerror = () => {
        dom.src = obj.value
      }
    },
    // 更新时触发
    componentUpdate(dom, obj, vnode) {
      obj.value = obj.value || require('@/assets/common/bigUserHeader.png')
      dom.src = dom.src || obj.value
    },
    // 销毁
    unbind(dom) {
      dom.src = null
    }
  },
  directiveForm: {
    inserted(dom, obj, vnode) {
      fn(dom, obj)
    },
    componentUpdate(dom, obj) {
      fn(dom, obj)
    }
  },

  clickOut: {
    inserted(dom, obj, vnode) {
      clickFn = (e) => {
        console.log('clickOut')
        if (!dom.contains(e.target)) {
          obj.value()
        }
      }
      document.addEventListener('click', clickFn)
    },
    unbind(dom, obj, vnode) {
      document.removeEventListener('click', clickFn)
    }
  }
}

export default {
  install(Vue) {
    Object.keys(directiveObj).forEach((item) => {
      Vue.directive(item, directiveObj[item])
    })
  }
}
